package encaps.resources;

import java.io.InputStream;
import java.util.Properties;

public class Config {

    public static final String CONFIG_PATH = "/encaps/resources/config.properties";
    public static final String DEFAULT_IMAGE = "defaultImage";
    public static final String USER_IMAGES_DIRECTORY = "userImagesDirectory";
    public static final String MOVIE_IMAGES_DIRECTORY = "movieImagesDirectory";
    public static final String PERSON_IMAGES_DIRECTORY = "personImagesDirectory";
    public static final String HOME_PAGE = "homePage";
    public static final String ROLE_PROVIDER_NAME = "roleProviderName";
    public static final String RECORDS_PER_PAGE = "recordsPerPage";
    public static final String PAGES_RUNNING_COUNT = "pagesRunningCount";
    private static Config config;
    private Properties props;

    private Config() {
        try {
            Class c = getClass();
            InputStream in = c.getResourceAsStream(CONFIG_PATH);
            props = new Properties();
            props.load(in);
        } catch (Exception e) {
            throw new RuntimeException(Labels.getLabel(Labels.CONFIG_ERROR_MESSAGE), e);
        }
    }

    public static void InitializeConfig() {
        if (config == null) {
            config = new Config();
        }
    }

    public static Config getConfig() {
        if (config == null) {
            config = new Config();
        }
        return config;
    }

    public static String getDefaultImage() {
        return getPropertyValue(DEFAULT_IMAGE);
    }

    public static String getUserImagesDirectory() {
        return getPropertyValue(USER_IMAGES_DIRECTORY);
    }

    public static String getMovieImagesDirectory() {
        return getPropertyValue(MOVIE_IMAGES_DIRECTORY);
    }

    public static String getPersonImagesDirectory() {
        return getPropertyValue(PERSON_IMAGES_DIRECTORY);
    }

    public static String getHomePage() {
        return getPropertyValue(HOME_PAGE);
    }

    public static String getRoleProviderName() {
        return getPropertyValue(ROLE_PROVIDER_NAME);
    }

    public static int getRecordsPerPage() {
        int recordsPerPage = Integer.parseInt(getPropertyValue(RECORDS_PER_PAGE));
        return recordsPerPage;
    }

    public static int getPagesRunningCount() {
        int pagesRunningCount = Integer.parseInt(getPropertyValue(PAGES_RUNNING_COUNT));
        return pagesRunningCount;
    }

    public static String getPropertyValue(String propertyName) {
        return getConfig().props.getProperty(propertyName);
    }
}
